<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;

class Cms_Member extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable, HasFactory;
    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'cms_member';

    public $timestamps = false;

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    protected $fillable = [
        'is_vip',
        'username',
        'realname',
        'sex',
        'mobile',
        'source',
        'birthday',
        'weixin',
        'address',
        'remark',
        'status'
    ];

    protected $hidden = ['pivot'];

    public function Mechanism()
    {
        return $this->belongsToMany('App\Models\Cms_Mechanism','cms_member_mechanism','member_id','mec_id');
    }

    public function hasOneMechanismMember()
    {
        return $this->hasOne('App\Models\Cms_Member_Mechanism', 'member_id', 'id');
    }

    public static function _generateSn($prefix = 'M')
    {
        $maxId = self::max('id');
        $maxId++;
        return $prefix . str_pad($maxId, 5, '0', STR_PAD_LEFT);
    }

    public function hasOneMemberCoud()
    {
        return $this->hasOne('App\Models\Cms_Member_Coud', 'mid', 'id');
    }

    public static function getFilter($filter)
    {
        return self::where(function ($query) use ($filter){
            if (isset($filter['keywords']) && !empty($filter['keywords'])) {
                $query->where('username', 'LIKE', '%' . $filter['keywords'] . '%' );
            }
            if (isset($filter['gender']) && !empty($filter['gender'])) {
                $query->where('gender',  $filter['gender'] );
            }
        });
    }

    public static function getFilterCoud($filter)
    {
        return self::whereHas('hasOneMemberCoud', function ($coud) use ($filter) {
            if (isset($filter['username']) && !empty($filter['username'])) {
                $coud->where('username', 'LIKE', '%' . $filter['username'] . '%' );
            }
        })->where(function ($query) use ($filter){
            if (isset($filter['username']) && !empty($filter['username'])) {
                $query->where('username', 'LIKE', '%' . $filter['username'] . '%' );
            }
        });
    }

    public function scopeGetMatchConditions($query,$filter)
    {
        return $query->where(function ($query) use ($filter){
            $this->applyMultiCondition($query, $filter, 'marry');
            $this->applyMultiCondition($query, $filter, 'education');
            $this->applyMultiCondition($query, $filter, 'job');
            $this->applyMultiCondition($query, $filter, 'salary');
            $this->applyMultiCondition($query, $filter, 'house');
            $this->applyMultiCondition($query, $filter, 'car');
            $this->applyMultiCondition($query, $filter, 'child');
            $this->applyMultiCondition($query, $filter, 'smoking');
            $this->applyMultiCondition($query, $filter, 'drinking');
            $this->applyMultiCondition($query, $filter, 'zodiac');
            $this->applyMultiCondition($query, $filter, 'constellation');
            $query->whereBetween('age', [$filter['min_age'],$filter['max_age']] );
            $query->whereBetween('height', [$filter['min_height'],$filter['max_height']] );
            $query->where('gender', $filter['gender'] );
            $query->where('state', '!=',3 );//匹配已经脱单的会员
        });
    }

    private function applyMultiCondition ($query,$filter, $targetField)
    {
        if(isset($filter[$targetField]) && !empty($filter[$targetField])) {
            $allowedValues = explode(',', $filter[$targetField]);
            $query->whereIn($targetField, $allowedValues);
        }
    }
}
